package cn.xutongxue.common.entity;

import cn.xutongxue.common.enums.Errors;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 通用响应类
 *
 * @param <T> 响应的数据类型
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ResponseResult<T> {

    private int code;
    private String message;
    private T data;

    public static <T> ResponseResult<T> success(T data) {
        return ResponseResult.<T>builder()
                .code(0)
                .message("成功")
                .data(data)
                .build();
    }

    public static <T> ResponseResult<T> success(T data, String message) {
        return ResponseResult.<T>builder()
                .code(0)
                .message(message)
                .data(data)
                .build();
    }

    public static <T> ResponseResult<T> error(int code, String message) {
        return ResponseResult.<T>builder()
                .code(code)
                .message(message)
                .data(null)
                .build();
    }

    public static <T> ResponseResult<T> error(String message) {
        return ResponseResult.<T>builder()
                .code(-1)
                .message(message)
                .data(null)
                .build();
    }

    public static <T> ResponseResult<T> error(Errors error) {
        return ResponseResult.<T>builder()
                .code(error.getCode())
                .message(error.getMessage())
                .data(null)
                .build();
    }
}
